package com.sq.partner.manager.controller;
import com.sq.partner.manager.service.AntPolicyService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import com.sq.partner.manager.util.StrUtil;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

    /**
     * 蚂蚁保单查询
     * SQ
     */
@Controller
@RequestMapping("/antPolicyController")
public class PolicyVoController {
    @Resource
    AntPolicyService antPolicyService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(){
        return "transManager/antPolicyQuery";
    }


    @RequestMapping(value = "/queryList",method = RequestMethod.POST)
    @ResponseBody
    public Page queryPolicyInfoByMap(HttpSession session,Integer pageIndex,String trdeNo,String policyNo,
									 String policyNo2,String tradeBeginTime,String tradeEndTime,String state,String mechan) throws Exception{
		Page page = checkBeforeQuery(session,pageIndex,trdeNo,policyNo,policyNo2,tradeBeginTime,tradeEndTime,state, mechan);
        return antPolicyService.queryPolicyPageByCondition(page);
    }

    public Page checkBeforeQuery(HttpSession session,Integer pageIndex,String trdeNo,String policyNo,
                                 String policyNo2,String tradeBeginTime,String tradeEndTime,String state, String mechan) throws Exception{
    	
        Page page = new Page();
        Map<String,Object> map = new HashMap<String, Object>();
        if(pageIndex != -1){
            page.setCurrent(pageIndex);
            map = SqlUtil.buildPageMap(page);
        }
          map.put("trde_no",trdeNo);
          map.put("state",state);
          map.put("policy_no",policyNo);
          map.put("policy_no2",policyNo2);
		  map.put("mechan",mechan);
        //交易日期
        if(StrUtil.isNotEmpty(tradeBeginTime)){
            tradeBeginTime = tradeBeginTime + "00:00:00";
        }
        if(StrUtil.isNotEmpty(tradeEndTime)){
            tradeEndTime = tradeEndTime + "23:59:59";
        }
        map.put("tradeBeginTime",tradeBeginTime);
        map.put("tradeEndTime",tradeEndTime);

        page.setType(map);
        return page;
    }
    
    @RequestMapping(value = "exportExcel")
	@ResponseBody
	public void exportExcel(HttpServletResponse response, HttpServletRequest request, String trdeNo,String policyNo,
            String policyNo2,String tradeBeginTime,String tradeEndTime,String state) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("trdeNo", trdeNo);
		map.put("policyNo", policyNo);
		map.put("policyNo2", policyNo2);
		map.put("tradeBeginTime", tradeBeginTime);
		map.put("tradeEndTime", tradeEndTime);
		map.put("state", state);
		//查数据
		List<Map<String,Object>> policyList = antPolicyService.queryPolicyByCondition(map);
		String fileName = "蚂蚁保单信息表";
		String sheetName = "蚂蚁保单信息信息";
		// 列数
		int columnNumber = 9;
		// 列宽
		int[] columnWidth = {5, 15, 15, 25, 10, 10, 20, 20, 20, };
		// 列名
		String[] columnName = {"序号", "蚂蚁订单号", "保单状态", "小保单号", "大保单号", "保费", "保额", "交易时间", "对账标识"};
		// 序号
		int count = 1;
		List<String[]> tempList = new ArrayList<String[]>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
		for (Map<String,Object> p : policyList) {
			tempList.add(new String[]{String.valueOf(count++), (String) p.get("TRDE_NO"), (String) p.get("STATE"), 
					(String) p.get("POLICY_NO"),(String) p.get("POLICY_NO2"), String.valueOf(p.get("PREMIUM")) ,String.valueOf(p.get("INSURED_AMOUNT")) ,
					p.get("TRADE_DATE")==null?"":df.format(p.get("TRADE_DATE"))
					  , (String) p.get("SIGN")});
		}
		// 海达信息的二维数组【列】【行】
		String[][] haidaInfos = tempList.toArray(new String[][]{});
		HSSFWorkbook wb = antPolicyService.generateHssWorkBook(sheetName, columnNumber, columnWidth, columnName, haidaInfos);
		response.setContentType("application/ms-excel;charset=utf-8");
		String userAgent = request.getHeader("user-agent").toLowerCase();
		if (userAgent.contains("msie") || userAgent.contains("trident") || userAgent.contains("edge")) {
			// IE浏览器
			fileName = URLEncoder.encode(fileName, "UTF-8");
		} else {
			// 非IE浏览器
			fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
		}
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
		OutputStream os = response.getOutputStream();
		try {
			wb.write(os);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			os.close();
		}
	}
}
